package com.beifu.manager.controller;

import com.beifu.manager.common.Const;
import com.beifu.manager.common.InnerPage;
import com.beifu.manager.model.AssetOutExample;
import com.beifu.manager.model.AssetOutTruthExample;
import com.beifu.manager.service.assetoutservice.AssetOutService;
import com.beifu.manager.service.assetouttruthservice.AssetOutTruthService;
import com.beifu.manager.service.organizeservice.OrganizeService;
import com.beifu.manager.shiro.ActiveUser;
import com.beifu.manager.vo.assetout.AssetOutDetailVo;
import com.mds.manager.utils.PageUtils;
import com.mds.manager.utils.ResponseHelp;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: licy
 * @Date :  2018/6/17 20:12
 * @Last Modified by: licy
 * @Last Modified time:  2018/6/17 20:12
 */
@Controller
public class WashManageController {

    private Logger logger = Logger.getLogger(AssetOutController.class);



    @Autowired
    private AssetOutService assetOutService;
    @Autowired
    private OrganizeService organizeService;
    @Autowired
    private AssetOutTruthService assetOutTruthService;


    /**
     * 洗涤单管理
     *
     * @param model
     * @return
     */
    @RequestMapping("/washingManage/list")
    public String getWashManage(Model model) {
        logger.info(">>跳转洗涤单管理列表...");
        return "/washing/manager/wash_manager_list";
    }




    @ResponseBody
    @RequestMapping(value= "washManage/getList",produces= MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    public String washManageListGet (Model model,HttpServletRequest request) {
        logger.info(">> 进入洗涤管理列表界面..");

        Subject subject = SecurityUtils.getSubject();
        ActiveUser activeUser = (ActiveUser)subject.getPrincipal();
        String organizeId = activeUser.getOrganizeId();

        String page = request.getParameter("pageNumber");
        PageUtils pageUtils = new PageUtils(page);


        int count = 0;
        String responseText = StringUtils.EMPTY;
        if(StringUtils.isNotBlank(organizeId)){
            count  = organizeService.countOrganizeService(organizeId);
            if(count==0||count>1){
                logger.error(">>>.没有查到对应用户的机构码,或机构码有多条");
                return ResponseHelp.responseText(pageUtils);
            }
            responseText = dealWashUser(request, pageUtils,organizeId);
        }else {
            logger.error(">>>.该用户没有配置机构码,默认为超级管理员。");
            responseText = dealWashUser(request, pageUtils,null);
        }

        logger.info("==========================================>responseText:"+responseText);
        return responseText;

    }

    /**
     * 查询用请求用户
     * @param request
     * @param page
     * @param organizeId
     * @return
     */
    private String dealWashUser(HttpServletRequest request, PageUtils page, String organizeId) {
        String query_assetOutId = request.getParameter("query_assetOutId");
        String query_assetOutStatus = request.getParameter("query_assetOutStatus");

        AssetOutExample example = new AssetOutExample();
        AssetOutExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("status asc");

        if(StringUtils.isNotBlank(organizeId)){
            int organizeIdInt = Integer.valueOf(organizeId);
            criteria.andDestOrganizeIdEqualTo(organizeIdInt);
        }


        if (!StringUtils.isEmpty(query_assetOutId)) {
            Integer query_assetOutId_int = Integer.valueOf(query_assetOutId);
            criteria.andIdEqualTo(query_assetOutId_int);
        }
        if (!StringUtils.isEmpty(query_assetOutStatus)) {
            Integer query_assetOutStatus_int = Integer.valueOf(query_assetOutStatus);
            criteria.andStatusEqualTo(query_assetOutStatus_int);
        }
        page.setQueryParames(example);
        page = assetOutService.getAssetOutPagesWithWashManage(page,true,false);
        String responseText = ResponseHelp.responseText(page);
        return responseText;
    }
    /**
     * 查看出库管理
     * @param request
     * @return
     */
    @RequestMapping("washManage/getassetOutDetail")
    public String getAssetOutDetail(HttpServletRequest request){
        String assetOutId = request.getParameter("id");


        AssetOutDetailVo assetOutDetail = assetOutService.getAssetOutDetail(assetOutId,true);
        request.setAttribute("assetOutDetailVo", assetOutDetail);

        return "washing/manager/wash_manager_detail";

    }

    @ResponseBody
    @RequestMapping(value = "washManage/getAssetOutTruthList" ,produces= MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    public String getAssetOutTruth(Model model, HttpServletRequest request){

        String assetOutId = request.getParameter("id");
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
//		String page = request.getParameter("pageNumber");
        PageUtils Page = new PageUtils();

        Page.setLimit(Integer.valueOf(limit));
        Page.setPage(Integer.valueOf(page));
        AssetOutTruthExample example = new AssetOutTruthExample();
        AssetOutTruthExample.Criteria criteria = example.createCriteria();
        Long assetOutIdint = Long.valueOf(assetOutId);
        criteria.andAssetOutIdEqualTo(assetOutIdint);
        Page.setQueryParames(example);

        PageUtils truthsWithPage = assetOutTruthService.getAssetOutTruthsWithPage(Page);

        //包装新的分页工具
        InnerPage innerPage = new InnerPage();
        innerPage.setCount(truthsWithPage.getCountNum());
        innerPage.setData(truthsWithPage.getList());
        String responseText = ResponseHelp.responseText(innerPage);

        return responseText;
    }

}